import requests,json,base64
__author__ = '飘零'
def merge_face(image_url_1, image_url_2, image_url, number):
    aa = "https://api-cn.faceplusplus.com/imagepp/v1/mergeface"
    f1 = open(image_url_1, 'rb')
    f1_64 = base64.b64encode(f1.read())
    f2 = open(image_url_2, 'rb')
    f2_64 = base64.b64encode(f2.read())
    data = {"api_key": 'd4ZNtShlnapO0vuXVb-DKdNU64rXQlmp', "api_secret": 'fc9LeI7io71rh8tvfUc3X082JPPlPrJD',
            "template_base64": f1_64,"merge_base64": f2_64, "merge_rate": number}# number表示换脸的相似度
    cc = requests.post(aa, data=data)
    req_con = cc.content.decode('utf-8')
    req_dict = json.JSONDecoder().decode(req_con)
    result = req_dict['result']
    imgdata = base64.b64decode(result)
    file = open(image_url, 'wb')
    file.write(imgdata)
    print('正在换脸中..............')
    print('换脸成功')
#要谁的脸:
image1 = r"11.jpg"
#替换脸的图片
image2 = r"2.jpg"
#生成新的图片
image = r"依然很美,依然很仙.jpg"
merge_face(image2, image1, image,90)
